package com.keon.jianbao.api;

import com.keon.jianbao.AppContext;
import com.keon.jianbao.bean.Expert;
import com.keon.jianbao.util.JsonUtils;
import com.keon.jianbao.util.StringUtils;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

/**
 * Created by keon on 2015/9/6.
 */
public class Api {
    public static void checkMobile(String mobile, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "User");
        params.put("api", "check_mobile");
        HashMap<String, String> login = new HashMap();
        login.put("user_mobile", mobile);
        params.put("jsonParams", JsonUtils.toJson(login));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void register(String mobile, String password, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "User");
        params.put("api", "register");
        HashMap<String, String> register = new HashMap();
        register.put("user_mobile", mobile);
        register.put("user_password", password);
        params.put("jsonParams", JsonUtils.toJson(register));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void login(String mobile, String password, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "User");
        params.put("api", "login");
        HashMap<String, String> jps = new HashMap();
        jps.put("user_mobile", mobile);
        jps.put("user_password", password);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void alter_password(String code, String oldP, String newP, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "User");
        params.put("api", "alter_password");
        HashMap<String, String> jps = new HashMap();
        params.put("code", code);
        jps.put("old_password", oldP);
        jps.put("new_password", newP);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void alter_info(String code, String hobby, String name, String address, String sex,
                                  String user_mobile, String user_face, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "User");
        params.put("api", "alter_info");
        params.put("code", code);
        HashMap<String, String> jps = new HashMap();
        if (!StringUtils.isEmpty(hobby)) {
            jps.put("user_hobby", hobby);
        }
        if (!StringUtils.isEmpty(name)) {
            jps.put("user_name", name);
        }
        if (!StringUtils.isEmpty(address)) {
            jps.put("user_address", address);
        }
        if (!StringUtils.isEmpty(sex)) {
            jps.put("user_gender", sex);
        }
        if (!StringUtils.isEmpty(user_mobile)) {
            jps.put("user_mobile", user_mobile);
        }
        if (!StringUtils.isEmpty(user_face)) {
            jps.put("user_face", user_face);
        }

        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void alter_user_face(String code, File avatar, AsyncHttpResponseHandler handler) throws FileNotFoundException {
        RequestParams params = new RequestParams();
        params.put("module", "User");
        params.put("api", "alter_user_face");
        params.put("code", code);
        params.put("user_face", avatar);
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void logout(String code, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "User");
        params.put("api", "loginout");
        params.put("code", code);
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void upload_activity_images(String code, File[] files, AsyncHttpResponseHandler handler) throws FileNotFoundException {
        RequestParams params = new RequestParams();
        params.put("module", "System");
        params.put("api", "upload_images");
        params.put("code", code);
        params.put("images[]", files);
        params.put("image_categoy", "Activity_Image");
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void activity_add(String code, String activity_name, String activity_place, String hold_unit, String linkman, String enroll_start, String enroll_end,
                                    String activity_start, String activity_end, String activity_intro, String[] activity_image, String linkinfo, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Activity");
        params.put("api", " activity_add");
        params.put("code", code);
        HashMap<String, Object> jps = new HashMap();
        jps.put("activity_name", activity_name);
        jps.put("activity_place", activity_place);
        jps.put("hold_unit", hold_unit);
        jps.put("linkman", linkman);
        jps.put("enroll_start", enroll_start);
        jps.put("enroll_end", enroll_end);
        jps.put("activity_start", activity_start);
        jps.put("activity_end", activity_end);
        jps.put("activity_intro", activity_intro);
        jps.put("activity_image", activity_image);
        jps.put("linkinfo", linkinfo);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void user_info(String code, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "User");
        params.put("api", " user_info");
        params.put("code", code);
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void user_info(String code, int user_id, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "User");
        params.put("api", " user_info");
        params.put("code", code);
        HashMap<String, Object> jps = new HashMap();
        jps.put("user_id", user_id);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void expert_lists(String code, int page, int size, int type, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Appraisal");
        params.put("api", "expert_lists");
        params.put("code", code);
        params.put("page", page);
        params.put("size", size);
        params.put("type", type);
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void appraisal_catalog(AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Appraisal");
        params.put("api", "appraisal_catalog");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void appraisal_age(AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Appraisal");
        params.put("api", "appraisal_age");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void upload_appraisal_images(String code, File[] files, AsyncHttpResponseHandler handler) throws FileNotFoundException {
        RequestParams params = new RequestParams();
        params.put("module", "System");
        params.put("api", "upload_images");
        params.put("code", code);
        params.put("images[]", files);
        params.put("image_categoy", "Apppraisal_Image");
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void appraisal_add(Expert expert, String appraisal_name, String appraisal_catalog, String appraisal_size, String appraisal_weight, String appraisal_age, String appraisal_desc, String[] appraisal_image, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Appraisal");
        params.put("api", "appraisal_add");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        HashMap<String, Object> jps = new HashMap();
        jps.put("expert_id", expert.getId());
        jps.put("expert_fee", expert.getExpert_fee());
        jps.put("expert_name", expert.getExpert_name());
        jps.put("appraisal_name", appraisal_name);
        jps.put("appraisal_catalog", appraisal_catalog);
        jps.put("appraisal_size", appraisal_size);
        jps.put("appraisal_weight", appraisal_weight);
        jps.put("appraisal_age", appraisal_age);
        jps.put("appraisal_desc", appraisal_desc);
        jps.put("appraisal_image", appraisal_image);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void appraisal_lists(int appraisal_status, int page, int size, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Appraisal");
        params.put("api", "appraisal_lists");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        params.put("appraisal_status", appraisal_status);
        params.put("page", "page");
        params.put("size", size);
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void appraisal_bespeak(String bespeak_name, String linkinfo, String appraisal_catalog, Expert expert, int status, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Appraisal");
        params.put("api", " appraisal_bespeak");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        HashMap<String, Object> jps = new HashMap();
        jps.put("status", status);
        if (expert != null) {
            jps.put("expert_id", expert.getId());
            jps.put("expert_fee", expert.getExpert_fee());
        }
        jps.put("bespeak_name", bespeak_name);
        jps.put("linkinfo", linkinfo);
        jps.put("appraisal_catalog", appraisal_catalog);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void appraisal_detail(int id, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Appraisal");
        params.put("api", " appraisal_detail");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        params.put("appraisal_id", id);
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void ads(AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "System");
        params.put("api", " ads");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void upload_tidings_images(String code, File[] files, AsyncHttpResponseHandler handler) throws FileNotFoundException {
        RequestParams params = new RequestParams();
        params.put("module", "System");
        params.put("api", "upload_images");
        params.put("code", code);
        params.put("images[]", files);
        params.put("image_categoy", "Tidings_Images");
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void add_tidings(String appraisal_category, String content, String tidings_price, String[] images, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Tidings");
        params.put("api", "add_tidings");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        HashMap<String, Object> jps = new HashMap();
        jps.put("appraisal_category", appraisal_category);
        jps.put("content", content);
        jps.put("images", images);
        jps.put("price", tidings_price);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void get_tidings(int type, int page, int size, String appraisal_category, AsyncHttpResponseHandler handler) {
        get_tidings(type, page, size, 0, appraisal_category, handler);
    }

    public static void get_tidings(int type, int page, int size, int user_id, String appraisal_category, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Tidings");
        params.put("api", " get_tidings");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        HashMap<String, Object> jps = new HashMap();
        jps.put("type", type);
        jps.put("page", page);
        jps.put("size", size);
        if (type == 3) {
            jps.put("user_id", user_id);
        }
        if (!StringUtils.isEmpty(appraisal_category)) {
            params.put("appraisal_category", appraisal_category);
            jps.put("appraisal_category", appraisal_category);
        }
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void add_upvote(int object_id, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Comment");
        params.put("api", "  add_upvote");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        HashMap<String, Object> jps = new HashMap();
        jps.put("object_id", object_id);

        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void tidings_detail(int tidings_id, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Tidings");
        params.put("api", "tidings_detail");
//        params.put("code", AppContext.getInstance().getProperty("user.code"));
        params.put("tidings_id", tidings_id);

        ApiHttpClient.post("user.php", params, handler);
    }

    public static void get_comment(int object_id, int type, int page, int size, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Comment");
        params.put("api", "get_comment");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        HashMap<String, Object> jps = new HashMap();
        jps.put("object_id", object_id);
        jps.put("type", type);
        jps.put("page", page);
        jps.put("size", size);

        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void add_comment(int object_id, int type, String content, int parent_id, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Comment");
        params.put("api", "add_comment");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        HashMap<String, Object> jps = new HashMap();
        jps.put("object_id", object_id);
        jps.put("type", type);
        jps.put("content", content);
        jps.put("parent_id", parent_id);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void get_upvote(int object_id, int page, int size, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Comment");
        params.put("api", "get_upvote");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        HashMap<String, Object> jps = new HashMap();
        jps.put("object_id", object_id);
        jps.put("size", size);
        jps.put("page", page);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void msg_code(String mobile, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "System");
        params.put("api", "msg_code");
//        params.put("code", AppContext.getInstance().getProperty("user.code"));
        HashMap<String, Object> jps = new HashMap();
        jps.put("mobile", mobile);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void activity_lists(int page, int size, int activity_type, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Activity");
        params.put("api", "activity_lists");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        params.put("page", page);
        params.put("size", size);
        params.put("activity_type", activity_type);

//        HashMap<String, Object> jps = new HashMap();
//        jps.put("mobile", mobile);
//        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void pay(String url, String json, AsyncHttpResponseHandler handler) throws UnsupportedEncodingException {
        ApiHttpClient.postJson(url, json, handler);
    }

    public static void async_binding(String openid, int type, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Asynclogin");
        params.put("api", "async_binding");
        HashMap<String, Object> jps = new HashMap();
        jps.put("openid", openid);
        jps.put("type", type);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }


    public static void async_login(String openid, int type, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Asynclogin");
        params.put("api", "async_login");
        HashMap<String, Object> jps = new HashMap();
        jps.put("openid", openid);
        jps.put("type", type);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void async_login_auto(String openid, int type, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Asynclogin");
        params.put("api", " async_login_auto");
        HashMap<String, Object> jps = new HashMap();
        jps.put("openid", openid);
        jps.put("type", type);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void give_star(int appraisal_id, int check, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Appraisal");
        params.put("api", "  give_star");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        HashMap<String, Object> jps = new HashMap();
        jps.put("appraisal_id", appraisal_id);
        jps.put("star", check);
//        jps.put("type", type);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void upload_suggest_images(String code, File[] files, AsyncHttpResponseHandler handler) throws FileNotFoundException {
        RequestParams params = new RequestParams();
        params.put("module", "System");
        params.put("api", "upload_images");
        params.put("code", code);
        params.put("images[]", files);
        params.put("image_categoy", "Suggest_Image");
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void suggest(String linkman, String moblie, String suggest, AsyncHttpResponseHandler handler) {
        suggest(linkman, moblie, suggest, null, handler);
    }

    public static void suggest(String linkman, String moblie, String suggest, String[] images, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "System");
        params.put("api", "  suggest");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        HashMap<String, Object> jps = new HashMap();
        jps.put("linkman", linkman);
        jps.put("moblie", moblie);
        jps.put("suggest", suggest);
        if (images != null && images.length > 0) {
            jps.put("images", images);
        }
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void get_changer(int object_id, String channel, int type, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Pay");
        params.put("api", "get_changer");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        HashMap<String, Object> jps = new HashMap();
        jps.put("object_id", object_id);
        jps.put("channel", channel);
        jps.put("type", type);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void expert_detail(int expert_id, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Appraisal");
        params.put("api", "expert_detail");
//        params.put("code", AppContext.getInstance().getProperty("user.code"));
        HashMap<String, Object> jps = new HashMap();
        jps.put("expert_id", expert_id);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void apk_update(AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "System");
        params.put("api", "apk_update");
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void get_msg(AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "System");
        params.put("api", "get_msg");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void msg_do_read(int id, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "System");
        params.put("api", "msg_do_read");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        HashMap<String, Object> jps = new HashMap();
        jps.put("id", id);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }

    public static void delete_tidings(int tidings_id, AsyncHttpResponseHandler handler) {
        RequestParams params = new RequestParams();
        params.put("module", "Tidings");
        params.put("api", "delete_tidings");
        params.put("code", AppContext.getInstance().getProperty("user.code"));
        params.put("tidings_id", tidings_id);
        HashMap<String, Object> jps = new HashMap();
        jps.put("tidings_id", tidings_id);
        params.put("jsonParams", JsonUtils.toJson(jps));
        ApiHttpClient.post("user.php", params, handler);
    }
}